package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.entity.Community;
import com.qcby.mycommunity_003.entity.Person;
import com.qcby.mycommunity_003.mapper.CommunityMapper;
import com.qcby.mycommunity_003.mapper.PersonMapper;
import com.qcby.mycommunity_003.service.PersonService;
import com.qcby.mycommunity_003.vo.CommunityVo;
import com.qcby.mycommunity_003.vo.PageVo;
import com.qcby.mycommunity_003.vo.PersonListVo;
import com.qcby.mycommunity_003.vo.PersonResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-27
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
   @Autowired
   private PersonMapper personMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Override
    public HashMap<String, Object> PersonList(PersonListVo personListVo) {
        //构造mybatis需要的page对象:封装当前页和每页的条数
        Page<Person> personPage=new Page<>(personListVo.getPage(),personListVo.getLimit());
        //条件构造器
        QueryWrapper<Person> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(personListVo.getUserName()),"user_name",personListVo.getUserName());
        queryWrapper.eq(personListVo.getCommunityId()!=null,"community_id",personListVo.getCommunityId());
        queryWrapper.eq(StringUtils.isNotBlank(personListVo.getMobile()),"mobile",personListVo.getMobile());
       //调用内置的分页
        Page<Person> resultPage = personMapper.selectPage(personPage, queryWrapper);
        PageVo pageVo=new PageVo();
        pageVo.setTotalCount(resultPage.getTotal());
        pageVo.setCurrPage(resultPage.getCurrent());
        pageVo.setPageSize(resultPage.getSize());
        pageVo.setTotalPage(resultPage.getPages());
        List<PersonResult> list=new ArrayList<PersonResult>();
        pageVo.setList(list);
        //对list进行封装
        for (Person person : resultPage.getRecords()) {
            PersonResult result=new PersonResult();
            BeanUtils.copyProperties(person,result);
            result.setCommunityName(communityMapper.selectById(person.getCommunityId()).getCommunityName());
            pageVo.getList().add(result);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("pageList",pageVo);
        return map;
    }
}
